package br.com.srv.teste;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import br.com.srv.config.ConfiguracaoSpring;
import br.com.srv.service.FFThread;

public class ServerReceiver {
	
	private static int countSocket = 0;
	private static int PORTA = 8000;
	
	public static void decrementarContador() {
		System.out.println(new Date() + " :: Conexões(s) Ativas: " + --countSocket);
	}
	public static void incrementarContador() {
		System.out.println(new Date() + " :: Conexões(s) Ativas: " + ++countSocket);
	}

	public static void main(String a[]) {
		try {
			
			if (a.length > 0 && a[0] != null) {
				PORTA = new Integer(a[0]);
			}
			
			ConfiguracaoSpring configuracaoSpring = new ConfiguracaoSpring();

			ServerSocket serverSocket = new ServerSocket(PORTA);
			System.out.println("Conectado na porta: " + PORTA);
			System.out.println("Aguardando conexão...");
			
			while (true) {				
				Socket conexao = serverSocket.accept();
				incrementarContador();
				
				FFThread ffThread = new FFThread(conexao);
				ffThread.setSerialVeiculo("Conexão n° " + countSocket);
				Thread t = new Thread(ffThread);
				t.start();
			}
		} catch (IOException ex) {
			ex.printStackTrace();			
		} catch(NumberFormatException ex2) {
			System.out.println("Digite o número da porta. Ex.: 8000");
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}
}